<template>
  <div class="auth-container">
    <el-card class="auth-card">
      <div class="logo-container">
        <h2 class="system-title">问卷系统</h2>
        <p class="system-description">创建、分发和分析您的调查问卷</p>
      </div>

      <el-tabs v-model="activeTab" stretch class="auth-tabs">
        <el-tab-pane label="创建者登录" name="creator">
          <el-form :model="form" :rules="rules" ref="loginForm" label-width="0">
            <el-form-item prop="username">
              <el-input
                  v-model="form.username"
                  placeholder="请输入用户名"
                  prefix-icon="el-icon-user"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                  type="primary"
                  @click="login('creator')"
                  class="login-btn"
              >
                进入创建模式
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>

        <el-tab-pane label="参与者登录" name="respondent">
          <el-form :model="form" :rules="rules" ref="loginForm" label-width="0">
            <el-form-item prop="username">
              <el-input
                  v-model="form.username"
                  placeholder="请输入用户名"
                  prefix-icon="el-icon-user"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                  type="success"
                  @click="login('respondent')"
                  class="login-btn"
              >
                进入填写模式
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/store/modules/auth.store'
import { ElMessage } from 'element-plus'

const router = useRouter()
const authStore = useAuthStore()
const activeTab = ref('creator')
const loginForm = ref(null)

const form = ref({
  username: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ]
}

const login = (role: 'creator' | 'respondent') => {
  loginForm.value?.validate((valid) => {
    if (valid) {
      authStore.login(form.value.username, role)
      ElMessage.success(`欢迎 ${form.value.username}`)

      if (role === 'creator') {
        router.push({ name: 'create' })
      } else {
        router.push({ name: 'home' })
      }
    }
  })
}
</script>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
}

.auth-card {
  width: 450px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.logo-container {
  text-align: center;
  margin-bottom: 30px;
}

.system-title {
  font-size: 24px;
  color: #303133;
  margin-bottom: 10px;
}

.system-description {
  color: #909399;
  font-size: 14px;
}

.auth-tabs {
  margin-top: 20px;
}

.login-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
}
</style>